"""
Case Type   : security-auditing
Case Name   : 统一审计策略：创建98个审计策略后继续创建显示提示信息
Create At   : 2025/5/9
Owner       : wan005
Description :
    1.创建用户表
    2.系统管理员用户创建资源标签
    3.系统管理员用户创建统一审计策略，过滤客户端gsql
Expect      :
    1.建表成功
    2.资源标签创建成功
    3.统一审计策略创建成功
History     :
"""
import os
import unittest
from yat.test import Node
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant


class Security(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'---{os.path.basename(__file__)} start---')
        self.userNode = Node(node='PrimaryDbUser')
        self.primary_root = Node(node='PrimaryRoot')
        self.sh_primy = CommonSH()
        self.common = Common()
        self.constant = Constant()
        self.table = 'table_security_auditing_0149'
        self.res_label = 'rl_security_auditing_0149'
        self.audit_policy = 'ap_security_auditing_0149'
        self.default_policy = self.common.show_param('enable_security_policy')

    def test_encrypted(self):
        text = '---pre1:设置enable_security_policy=on;expect:成功---'
        self.logger.info(text)
        result = self.sh_primy.execute_gsguc('reload',
                                             self.constant.GSGUC_SUCCESS_MSG,
                                             f'enable_security_policy=on')
        self.assertEqual(True, result, '执行失败' + text)

        text = '---step1:创建用户表;expect:成功---'
        self.logger.info(text)
        sql_cmd1 = f'drop table if exists {self.table};' \
                   f'create table {self.table}(id int, name varchar(10));'
        res1 = self.sh_primy.execut_db_sql(sql_cmd1)
        self.logger.info(res1)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, res1, '执行失败' + text)

        text = '---step2:系统管理员用户创建资源标签;expect:成功---'
        self.logger.info(text)
        sql_cmd2 = f'drop resource label if exists {self.res_label};' \
            f'create resource label {self.res_label} add table({self.table});'
        res2 = self.sh_primy.execut_db_sql(sql_cmd2)
        self.logger.info(res2)
        self.assertIn('CREATE RESOURCE LABEL', res2, '执行失败' + text)

        text = '---step3:系统管理员用户创建统一审计策略，过滤用户1;expect:成功---'
        self.logger.info(text)
        for i in range(1, 99):
            audit_name = self.audit_policy + f'_0{i}'
            sql_cmd3 = f'create audit policy {audit_name} privileges alter' \
            f' on label({self.res_label}) filter on app(gsql);'
            res3 = self.sh_primy.execut_db_sql(sql_cmd3)
            self.logger.info(res3)
        audit_name = self.audit_policy + f'_099'
        sql_cmd4 = f'create audit policy {audit_name} privileges alter' \
                   f' on label({self.res_label}) filter on app(gsql);'
        res4 = self.sh_primy.execut_db_sql(sql_cmd4)
        self.logger.info(res4)
        self.assertIn('WARNING:  Too many policies, adding new policy is not '
                      'recommended', res4, '执行失败' + text)
        self.assertIn('CREATE AUDIT POLICY', res4, '执行失败' + text)

    def tearDown(self):
        text = '--step1:恢复参数默认值;expect:成功--'
        self.logger.info(text)
        res1 = self.sh_primy.execute_gsguc('reload',
                            self.constant.GSGUC_SUCCESS_MSG,
                            f'enable_security_policy={self.default_policy}')
        self.assertEqual(True, res1, '执行失败' + text)

        text = '--step2:删除审计策略;expect:成功--'
        self.logger.info(text)
        for i in range(1, 100):
            audit_name = self.audit_policy + f'_0{i}'
            sql_cmd2_1 = f'drop audit policy {audit_name};'
            res2_1 = self.sh_primy.execut_db_sql(sql_cmd2_1)
            self.logger.info(res2_1)
        sql_cmd2_2 = f"select count(*) from gs_auditing_policy where polname " \
                   f"like '{self.audit_policy}%'"
        res2_2 = self.sh_primy.execut_db_sql(sql_cmd2_2)
        self.logger.info(res2_2)
        self.common.equal_sql_mdg(res2_2, 'count', '0', '(1 row)', flag=1)

        text = '--step3:删除资源标签及用户表;expect:成功--'
        self.logger.info(text)
        sql_cmd3 = f'drop resource label if exists {self.res_label};' \
            f'drop table if exists {self.table} cascade;'
        res3 = self.sh_primy.execut_db_sql(sql_cmd3)
        self.logger.info(res3)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, res3, '执行失败' + text)
        self.logger.info(f'---{os.path.basename(__file__)} start---')
